C Clearly: An Introduction to C Programming by P K McBride

C Clearly: An Introduction to C Programming by P K McBride

Author:P K McBride [McBride, P K]
Language: eng
Format: mobi
Tags: programming, C language
Publisher: macbride.org
Published: 2011-06-07T16:00:00+00:00


14 Strings and Numbers

This chapter is intended mainly as a refresher. It doesn’t introduce any new concepts, but does reuse a number of techniques in a variety of new ways. The focus is on converting between numbers and strings of digits - in both directions, and in different bases. We’ll develop a couple of new functions, and a larger program that will produce hex dumps of files.

C is quite well supplied with functions for converting strings of digits to numbers, and for entering or displaying values in different bases. atof(), atoi() and atol() will take strings and turn them into float, int, and long values respectively. The printf() and scanf() formatters “%o”,”%x” and “%X” will write and read numbers in octal and hexadecimal forms. So why develop any more functions? The answer is, partly that it is good practice, and partly because the standard set doesn’t cover all the things you might want to do.

Strings to Numbers

The strnum() function, shown in Figure 14.1, takes in a string of digits - in any base from 2 to 16 - and returns their denary (base 10) value. Pass it down the string “101010”, specify base 2 (binary), and it will return the value 42. Give it “-123” in base 8 (octal), and you will get -83. It will also, of course, work equally well for base 10.

The routine is a development of the geti() function, given in Chapter 10, with three main changes. The first is that the line:

num = num * 10 + digit;

is replaced by the variable line:

num = num * base + digit;

So, if you started with “2B” in base 16, on the second pass, with num = 2 and digit = 11, the line gives us 2 * 16 + 11 = 43.

The second change is necessary to cope with the additional hexadecimal digits, ‘A’ to ‘F’. Most inconsiderately, the designers of character sets did not locate these directly after ‘0’ to ‘9’.

if (s[n] >= ‘A’ && s[n] <= ‘F’)

digit = (s[n] - ‘A’ + 10);

By subtracting ‘A’, the digits ‘A’ to ‘F’ are reduced to the values 0 to 5; the addition of 10 pushes them to their proper values. If the program was only to be used with ASCII characters, this line could be simplified to:

digit = s[n] - 55;

The characters ‘a’ to ‘f’ might also be used for hex numbers, so we must accommodate these as well. One solution would be to repeat the same if(.. lines, but with lower case letters. A briefer, if more cryptic, alternative is to use AND masking.

s[n] = s[n] & 223;

The ampersand & is acting here as a bitwise AND operator - that is, it works through two bytes, bit by bit, with the resultant bits being set to 1 if they are in both original bytes. It is the same as the logical operator && in that the overall test proves true only if both tests are true:

0 & 0 = 0 FALSE && FALSE = FALSE

0 &



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.